package com.yuanfengerp.SalesOrder.service.impl;

import com.yuanfengerp.SalesOrder.dao.INewOrdersDao;
import com.yuanfengerp.SalesOrder.dao.ISalesOrderDao;
import com.yuanfengerp.SalesOrder.model.NewOrders;
import com.yuanfengerp.SalesOrder.model.SalesOrder;
import com.yuanfengerp.SalesOrder.service.INewOrdersService;
import com.yuanfengerp.basicInfoManage.paymode.moudel.PayMethodInfo;
import com.yuanfengerp.goodsInfoManage.model.ProductManagement;
import com.yuanfengerp.publicUtil.Result;
import com.yuanfengerp.publicUtil.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;

@Service
public class NewOrdersServiceImpl implements INewOrdersService {
    @Autowired
    private INewOrdersDao inod;
    @Autowired
    private ISalesOrderDao isod;
    @Override
    public ArrayList<ProductManagement> getProductManagement(String warehouse_name , String pmName) {
        ArrayList<ProductManagement> list = inod.getProductManagement(warehouse_name , pmName);
        return list;
    }

    @Override
    public Result<ProductManagement> updateProductManagement(ProductManagement productManagement) {
        int pm = inod.updateProductManagement(productManagement);
        return pm > 0 ? Result.success(productManagement) : Result.failure(400,"修改失败");
    }

    @Override
    public Result<NewOrders> insertNewOrders(NewOrders newOrders) {
        SalesOrder so = new SalesOrder();
        if(newOrders.getAll_quantity() == 0){
            return Result.failure(400,"订单数量不能为0");
        }
        if(newOrders.getPay_the_amount() == 0){
            return Result.failure(400,"支付金额不能为0");
        }
        if(newOrders.getTotal_invoice_amount() != newOrders.getPay_the_amount()){
            return Result.failure(400,"支付金额与订单金额不相等");
        }
        SnowflakeIdWorker sfiw = new SnowflakeIdWorker(1);
        newOrders.setProd_id(sfiw.nextId());
        so.setProd_id(newOrders.getProd_id());
        so.setWarehouse_name(newOrders.getWarehouse_name());
        so.setCustomer_id(newOrders.getCustomer_id());
        so.setAction_time(new Date());
        so.setAuditor("admin");
        so.setQuantity(newOrders.getAll_quantity());
        so.setStatus(0);
        so.setOperator("admin");
        isod.insertSalesOrder(so);
        return inod.insertNewOrders(newOrders) > 0 ? Result.success(newOrders) : Result.failure(400,"添加失败");
    }

    @Override
    public ArrayList<PayMethodInfo> getPayMethodInfo() {
        ArrayList<PayMethodInfo> payMethodInfo = inod.getPayMethodInfo();
        return payMethodInfo;
    }
}
